package jude.java.money.ant;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.tools.ant.Task;

public class Base extends Task {
	
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}
	
	public String getAbsolutePath(String path) {
		return getProject().getBaseDir() + "\\" + path;
	}
	
	public static StringBuilder read(InputStream in, String encode) {
		try {
			StringBuilder r = new StringBuilder();
			Reader reader = new InputStreamReader(in, encode);
			char[] buffer = new char[4096];
			int n;
			while ((n = reader.read(buffer)) > 0) {
				r.append(buffer, 0, n);
			}
			return r;
		} catch (Exception e) {
		}
		return null;
	}

	public static String readFile(String filePath, String encode) {
		File f = new File(filePath);
		try {
			return read(new FileInputStream(f), encode).toString();
		} catch (IOException e) {
			return null;
		}
	}
	
	public static int getInteger(String str, int def) {
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException e) {
		}
		return def;
	}
	
	protected String get(String propertyName) {
		return getProject().getProperty(propertyName);
	}
	
	protected void set(String propertyName, String propertyValue) {
		getProject().setProperty(propertyName, propertyValue);
	}
}
